package com.xq.demo;

import cn.hutool.core.lang.Console;
import cn.hutool.http.webservice.SoapClient;
import com.xq.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;

import javax.xml.soap.*;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import java.util.List;

public class WebserviceDemo {
    public static void main(String[] args) {
       /* SoapClient client = SoapClient.create("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx")
                // 设置要请求的方法，此接口方法前缀为web，传入对应的命名空间
                .setMethod("web:getSupportProvince", "http://WebXml.com.cn/")
        // 设置参数，此处自动添加方法的前缀：web
        .setParam("byProvinceName", "北京");

        // 发送请求，参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
        String str = client.send(true);
        System.out.println(str);*/

       // 新建客户端
        SoapClient client = SoapClient.create("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx")
                // 设置要请求的方法，此接口方法前缀为web，传入对应的命名空间
                .setMethod("web:getSupportProvince", "http://WebXml.com.cn/")
                // 设置参数，此处自动添加方法的前缀：web
                .setParam("theIpAddress", "127.0.0.1");
        // 发送请求，参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
        String str = client.send(true);
        System.out.println(str);
        List list= XmlUtils.parseObject(str,"getSupportProvinceResponse","java.lang.String");
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(0));
        }
        /*try {
            SOAPMessage msg = formatSoapString(str);
            SOAPBody body = msg.getSOAPBody();
            Iterator<SOAPElement> iterator = body.getChildElements();
            PrintBody(iterator, null);
        } catch (Exception e) {
            e.printStackTrace();
        }*/


    }



    /**
     * 把soap字符串格式化为SOAPMessage
     *
     * @param soapString
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static SOAPMessage formatSoapString(String soapString) {
        MessageFactory msgFactory;
        try {
            msgFactory = MessageFactory.newInstance();
            SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
                    new ByteArrayInputStream(soapString.getBytes("UTF-8")));
            reqMsg.saveChanges();
            return reqMsg;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 解析soapXML
     * @param soapXML
     * @return
     */
    public static void parseSoapMessage(String soapXML) {
        try {
            SOAPMessage msg = formatSoapString(soapXML);
            SOAPBody body = msg.getSOAPBody();
            Iterator<SOAPElement> iterator = body.getChildElements();
            parse(iterator);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 解析soap xml
     * @param iterator
     * @param resultBean
     */
    private static void parse(Iterator<SOAPElement> iterator) {
        while (iterator.hasNext()) {
            SOAPElement element = iterator.next();
            if ("ns:BASEINFO".equals(element.getNodeName())) {
                continue;
            } else if ("ns:MESSAGE".equals(element.getNodeName())) {
                Iterator<SOAPElement> it = element.getChildElements();
                SOAPElement el = null;
                while (it.hasNext()) {
                    el = it.next();
                    if ("RESULT".equals(el.getLocalName())) {
                        System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
                    } else if ("REMARK".equals(el.getLocalName())) {
                        System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
                    } else if ("XMLDATA".equals(el.getLocalName())) {
                        System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
                    }
                }
            } else if (null == element.getValue()
                    && element.getChildElements().hasNext()) {
                parse(element.getChildElements());
            }
        }
    }


    private static void PrintBody(Iterator<SOAPElement> iterator, String side) {
        while (iterator.hasNext()) {
            Object o=iterator.next();
            if(o!=null)  {
                SOAPElement element=null;
                try{
                    element = (SOAPElement) o;
                   // System.out.println("Node Name:" + element.getNodeName());
                    if(StringUtils.isNotBlank(element.getValue())){
                        System.out.println(element.getValue());
                    }

                }catch(Exception e){}
                if ( element !=null ) {
                    PrintBody(element.getChildElements(), side + "-----");
                }
            }
        }
    }

}
